<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form
      ref="form"
      label-width="140px"
      :model="formData"
      :rules="formRules"
    >
      <el-form-item label="连接类型" prop="">
        <el-select v-model="value" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="连接地址" prop="des">
        <el-input />
      </el-form-item>
      <el-form-item label="连接描述" prop="url">
        <el-input
          v-model="formData.description"
          type="textarea"
          :rows="1"
          placeholder="请输入"
        />
      </el-form-item>
      <!-- <el-form-item label="连接描述"> -->
      <!-- 树 -->
      <!-- <el-tree
          ref="tree"
          :data="allTreeData"
          :props="{ label: 'title' }"
          show-checkbox
          node-key="id"
          @check="checkTree"
        /> -->

      <el-row type="flex" justify="center">
        <el-button
          type="primary"
          size="small"
          @click="onSubmit"
        >提交</el-button>
        <el-button size="small" @click="dialogShow = false">关闭</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { addadvLinkInfoApi, updateadvLinkInfoApi } from '@/api/adv_Link'
// import { getSysMenuListApi } from '@/api/sys_menu'
export default {
  name: 'AdvPos',
  data() {
    return {
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        name: '',
        description: '',
        menuids: []
      },
      // 表单验证规则
      formRules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        description: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      options: [
        {
          value: '选项2',
          label: '文字链接'
        }, {
          value: '选项3',
          label: '图片链接'
        }],
      value: ''
      // 树的数据
      // allTreeData: []
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  created() {
    // this.getTreeData()
  },
  methods: {

    // 提交行为
    async onSubmit() {
      // 提交之前获取一下树上的对钩和半钩
      this.formData.menuids = [
        ...this.$refs.tree.getCheckedKeys(),
        ...this.$refs.tree.getHalfCheckedKeys()
      ]
      if (!this.formData.menuids.length) {
        return this.$message.warning('请勾选')
      }
      // 表单的校验
      await this.$refs.form.validate()
      // 加工处理表单数据(拷贝一份再加工)
      // 请求的发生
      this.formData.id
        ? await updateadvLinkInfoApi(this.formData)
        : await addadvLinkInfoApi(this.formData)
      // 提示效果
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      // 关闭弹框
      this.dialogShow = false
      // 列表更新
      this.$emit('updateList')
    },
    // 弹框关闭
    onClose() {
      // 表单重置
      this.formData = {
        name: '',
        description: '',
        menuids: []
      }
      // 移除校验
      this.$refs.form.clearValidate()
      // 打钩也不要了
      // this.$refs.tree.setCheckedKeys([])
    }
  }
}
</script>
<style scoped></style>
